package com.xii.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xii.constant.MessageConstant;
import com.xii.entity.Result;
import com.xii.pojo.OrderSetting;
import com.xii.service.OrderSettingService;
import com.xii.utils.POIUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/ordersetting")
public class OrderSettingController {

    @Reference
    private OrderSettingService ordersettingService;


    /**
     * 文件上传
     *
     * @param excelFile 上传的文件
     * @return
     */
    @RequestMapping("/upload")
    @ResponseBody
    public Result upload(MultipartFile excelFile) {
        try {
            //读取文件得到一个数组集合
            List<String[]> readExcel = POIUtils.readExcel(excelFile);
            List<OrderSetting> orderSettings = new ArrayList<>();
            //  遍历集合将读取的数据添加到集合
            for (String[] readExcelal : readExcel) {
                String orderdate = readExcelal[0];
                String ordernum = readExcelal[1];
                OrderSetting orderSetting = new OrderSetting(new Date(orderdate), Integer.parseInt(ordernum));
                orderSettings.add(orderSetting);
            }
            //集合添加到数据库
            ordersettingService.addOrderSetting(orderSettings);
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }

    @RequestMapping("/getOrderSettingByMonth")
    @ResponseBody
    public Result getOrderSettingByMonth(String date) {
        try {
            List<Map> orderSettinglist= ordersettingService.getOrderSettingByMonth(date);
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, orderSettinglist);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
        }
    }

    @RequestMapping("/editNumberByDate")
    @ResponseBody
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting) {
        try {
            ordersettingService.editNumberByDate(orderSetting);
            return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }
    }
}
